<template>
  <div class="progress-bar" ref="progressBar" @click="progressClick">
    <!-- 总进度条 -->
    <div class="bar-inner">
      <!-- 前进多少 -->
      <div class="progress" ref="progress"></div>
      <!-- 添加 prevent 阻止浏览器默认行为 -->
      <div class="progress-btn-wrapper" ref="progressBtn" @touchstart.prevent="progressTouchStart" @touchmove.prevent="progressTouchMove" @touchend="progressTouchEnd">
        <!-- 小球 -->
        <div class="progress-btn" ref="progressBtn_2"></div>
      </div>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
import { prefixStyle } from 'common/js/dom'
const transform = prefixStyle('transform')
export default {
  props: {
    percent: {
      type: Number,
      default: 0
    }
  },
  created () {
    this.touch = {}
  },
  methods: {
    // 当手指触摸屏幕时候触发
    progressTouchStart (e) {
      // console.log(e)
      // 初始化完毕旗帜 表示已经初始化完毕
      this.touch.initiated = true
      // 记录手指点击的为位置 手指触摸屏幕位置
      this.touch.startX = e.touches[0].clientX
      // 记录手指触摸屏幕一瞬间 已经偏移了多少
      this.touch.left = this.$refs.progress.clientWidth
    },
    // 当手指在屏幕上滑动的时候连续触发
    progressTouchMove (e) {
      // console.log(e)
      // 判断是否已经初始化
      if (!this.touch.initiated) {
        return false
      }
      // 计算移动的距离
      const deltaX = e.touches[0].clientX - this.touch.startX
      // 不能小于零 并且不能大于总进度条距离
      const offsetWidth = Math.min(this.$refs.progressBar.clientWidth - this.$refs.progressBtn_2.offsetWidth, Math.max(0, this.touch.left + deltaX))
      this._offset(offsetWidth)
    },
    // 当手指在屏幕上离开的时候触发
    progressTouchEnd () {
      // 松开手指 重置
      this.touch.initiated = false
      this._triggerPercent()
    },
    // 用户点击进度条
    progressClick (e) {
      // console.log(e)
      // getBoundingClientRect 该方法可以获取元素的上下左右位置
      const rect = this.$refs.progressBar.getBoundingClientRect().x
      const offsetWidth = e.clientX - rect
      this._offset(offsetWidth)
      this._triggerPercent()
    },
    _triggerPercent () {
      // 进度条总宽度 - 小球宽度 在手指离开屏幕的一瞬间 我们计算出用户拖动的比列
      const barWidth = this.$refs.progressBar.clientWidth - this.$refs.progressBtn_2.offsetWidth
      // 计算拖动百分比
      const percent = this.$refs.progress.clientWidth / barWidth
      // 告诉外面 用户拖动了多少
      this.$emit('percentChange', percent)
    },
    _offset (offsetWidth) {
      this.$refs.progress.style.width = offsetWidth + 'px'
      this.$refs.progressBtn.style[transform] = `translate3d(${offsetWidth}px, 0, 0)`
    }
  },
  watch: {
    percent (newPercent) {
      if (newPercent > 0 && !this.touch.initiated) {
        // 进度条总宽度 - 小球宽度
        const barWidth = this.$refs.progressBar.clientWidth - this.$refs.progressBtn_2.offsetWidth
        const offsetWidth = barWidth * newPercent
        this._offset(offsetWidth)
      }
    }
  }
}
</script>

<style lang="stylus" scoped rel="stylesheet/stylus">
  @import '~common/stylus/variable';

  .progress-bar {
    height: 30px;
    width: 100%;
    display: flex;
    align-items: center;

    .bar-inner {
      height: 4px;
      background: rgba(0, 0, 0, 0.3);
      width: 100%;
      position: relative;

      .progress {
        position: absolute;
        height: 100%;
        background: $color-theme;
      }

      .progress-btn-wrapper {
        position: absolute;
        width: 30px;
        height: 30px;
        left: -7px;
        top: -13px;

        .progress-btn {
          box-sizing: border-box;
          width: 16px;
          height: 16px;
          background-color: $color-theme;
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate3d(-50%, -50%, 0);
          border: 3px solid #fff;
          border-radius: 50%;
        }
      }
    }
  }
</style>
